package XPathProj;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Node;

public class NodeComparator {

	// classes implementing operators
	private class Equal implements BooleanOperator {
		
		public boolean compareNodes(Node firstNode, Node secondNode) {
			
			return firstNode.isEqualNode(secondNode);
		}
	}
	
	private class NotEqual implements BooleanOperator {
		
		public boolean compareNodes(Node firstNode, Node secondNode) {
			
			return !firstNode.isEqualNode(secondNode);
		}
	}
	
	// map holding theses classes
	private Map<String, BooleanOperator> comparators;
	
	public NodeComparator() {
		
		// allocate map to hold the comparators
		comparators = new HashMap<String, BooleanOperator>();
		
		// add comparators
		comparators.put("=", new Equal());
		comparators.put("!=", new NotEqual());
	}
	
	// TODO UPDATE COMMENT
	/**
	 * Compares given nodes according to implementing class's operator
	 * @param firstNode- first node to be compared
	 * @param secondNode- second node to be compared
	 * @return result of implementing class's boolean operator
	 */
	public boolean compare(Node firstNode, Node secondNode, String operator) {

		return comparators.get(operator).compareNodes(firstNode, secondNode);
	}
}